import 'dart:io';

import 'package:common_utils/common_utils.dart';
import 'package:dio/dio.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/login/model/models.dart';
import 'package:nft_shop/market/model/models.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/my/page/set/help/model/HelpDetailModel.dart';
import 'package:nft_shop/my/page/set/help/model/HelpModel.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/http_api.dart';
import 'package:nft_shop/sift/model/models.dart';
import 'package:nft_shop/util/log_utils.dart';

class ServiceRepository {
  //轮播 公告
  static Future fetchBanners({int? type, int? jump_type = 2}) async {
    var response = await http.get(HttpApi.NoticeList,
        queryParameters: {"type": type, "jump_type": jump_type});
    return response.data['list']["data"]
        .map<Article>((item) => Article.fromJson(item))
        .toList();
  }

  /// 登录
  static Future login(
      {required String username,
      required String password,
      required String mobile_mark}) async {
    print("---登录参数 ${username}--${password}--${mobile_mark}");

    var response = await http.post(HttpApi.authLogin, data: {
      'username': username,
      'password': password,
      'mobile_mark': mobile_mark,
      'verify_code': "",
      'loing_type': "2"
    });
    return LoginModel.fromJson(response.data);
  }

  //
  // /// ID登录
  // static Future loginID(int cid, String mobile) async {
  //   print("---传入参数---${cid}---${mobile}");
  //   var response = await http.post(HttpApi.AccountLogin, data: {
  //     'id': cid,
  //     'mobile': mobile,
  //   });
  //   return AuthLoginEntity.fromJson(response.data);
  // }

  //绑定手机号邮箱
  static bindMobileEmail(String username, String code, String type) async {
    print("--绑定参数---${username}--${code}---${type}");
    await http.post(HttpApi.bindMobileEmail, data: {
      'username': username,
      'type': type,
      'verify_code': code,
    });
  }

  /// 验证码登录
  // static Future loginSms(String username, String code,String mobile_mark) async {
  //   print("--验证码登录方式--${username}--${code}--${mobile_mark}");
  //   var response = await http.post(HttpApi.authLogin, data: {
  //     'username': username,
  //     'password': "",
  //     'mobile_mark': mobile_mark,
  //     'verify_code': code,
  //     'loing_type': "1"
  //   });
  //   return AuthLoginEntity.fromJson(response.data);
  // }

  /// 邮箱登录
  // static Future loginEmail(
  //     {required String username,
  //       required String password,
  //       required String mobile_mark}) async {
  //   print("---登录参数 ${username}--${password}--${mobile_mark}");
  //
  //   var response = await http.post(HttpApi.authLogin, data: {
  //     'username': username,
  //     'password': password,
  //     'mobile_mark': mobile_mark,
  //     'verify_code': "",
  //     'loing_type': "3"
  //   });
  //   return AuthLoginEntity.fromJson(response.data);
  // }

  //账号注册
  static register({
    String? username,
    String? code,
    String? password,
    String? referee,
  }) async {
    var response = await http.post(HttpApi.register, data: {
      "username": username,
      "verify_code": code,
      "password": password,
      "referee": referee
    });
    return LoginModel.fromJson(response.data);
  }

  //修改密码 找回密码
  static changePassword({
    required String loginId,
    required String password,
    required String repassword,
    required String verify_code,
    required String type,
  }) async {
    Log.e(
        "loginId-${loginId}--password--${password}--repassword--${repassword}--verify_code--${verify_code}");
    await http.post(HttpApi.setPassword, data: {
      "username": loginId,
      "password": password,
      "repassword": repassword,
      "verify_code": verify_code,
      "type": type
    });
  }

  //修改交易密码
  static setSafetyCode(
      {required String mobile,
      required String safety_code,
      required String resafecode,
      required String verify_code}) async {
    Log.v(
        "mobile-${mobile}--safety_code--${safety_code}--resafecode--${resafecode}--verify_code--${verify_code}");
    await http.post(HttpApi.setSafetyCode, data: {
      "username": mobile,
      "safety_code": safety_code,
      "re_safe_code": resafecode,
      "verify_code": verify_code,
      "type": "1"
    });
  }

  //获取验证码
  static shayuSms(String mobile,String smsCode) async {
      await http.post(HttpApi.shayuSms, data: {"username": mobile, "type": 1,"imgcode":smsCode});
  }

  //验证码
  static validatePassword(String password) async {
    await http.post(HttpApi.validatePassword, data: {"password": password});
  }

  //用户信息
  static Future user() async {
    var response = await http.get(HttpApi.user);
    return Member.fromJson(response.data["member"]);
  }

  //用户消息列表
  static Future userMessage({
    int? page_size,
    int? page,
  }) async {
    var response = await http.get(
      HttpApi.memberNoticeList,
    );
    return response.data["data"]
        .map<MemberNotice>((item) => MemberNotice.fromJson(item))
        .toList();
  }

  //用户消息修改已读
  static memberNoticeUpdate(int id) async {
    await http.post(HttpApi.memberNoticeUpdate, data: {"id": id});
  }

  //取消订单
  static cancelOrder(String id) async {
    await http.post(HttpApi.cancelOrder, data: {"id": id});
  }

  //分享好友
  static Future inviteFriends() async {
    var response = await http.post(HttpApi.inviteFriends);
    return InviteFriendsModel.fromJson(response.data);
  }

  //我的直推
  static Future getInviteList({
    int? pageSize,
    int? page,
  }) async {
    var response = await http.get(HttpApi.getInviteList,
        queryParameters: {"page": page, "page_size": pageSize});
    return response.data["data"]
        .map<InviteListModel>((item) => InviteListModel.fromJson(item))
        .toList();
  }

  //我的团队
  static Future getTeamList({
    int? pageSize,
    int? page,
  }) async {
    var response = await http.get(HttpApi.getTeamList,
        queryParameters: {"page": page, "page_size": pageSize});
    return response.data["data"]
        .map<InviteListModel>((item) => InviteListModel.fromJson(item))
        .toList();
  }

  //我的NFT
  static Future mySpecialList(
      {int? pageSize, int? page, int? bigType, int? smallType}) async {
    var response = await http.get(HttpApi.mySpecialList, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "big_type": bigType,
      "small_type": smallType
    });
    return MyGoodsSpecialDataModel.fromJson(response.data);
  }

  // //用户内容
  // static Future syncData() async {
  //   var response = await http.post(HttpApi.syncData, data: {"is_sync": "true"});
  //   return SyncData.fromJson(response.data);
  // }
  //
  //用户信息修改
  static setMemberInfo(portrait, nickname, introduce) async {
    await http.post(HttpApi.setMemberInfo, data: {
      "portrait": portrait ?? "",
      "nickname": nickname ?? "",
      "introduce": introduce ?? ""
    });
  }

  //
  //
  //登陆记录
  static Future getloginLog(int page) async {
    var response = await http.get(HttpApi.getloginLog,
        queryParameters: {"page": page, "page_size": 20});
    return response.data["data"]
        .map<LoginLog>((item) => LoginLog.fromJson(item))
        .toList();
  }

  //我的订单
  static Future orderList({required int page, required int type}) async {
    var response = await http.get(HttpApi.orderList,
        queryParameters: {"type": type, "page": page, "page_size": 20});
    print("我的订单---${response}");
    return response.data["list"]["data"]
        .map<OrderListModel>((item) => OrderListModel.fromJson(item))
        .toList();
  }

  //订单性情
  static Future orderInfo({required int id}) async {
    var response = await http.get(HttpApi.orderInfo, queryParameters: {
      "id": id,
    });
    return response.data["list"]["data"]
        .map<OrderListModel>((item) => OrderListModel.fromJson(item))
        .toList();
  }

  //我的专辑/盲盒/藏品
  static Future myGoodsSpecialList(
      {required int page,
      required int pageSize,
      required int bigType,
      required int smallType}) async {
    var response = await http.get(HttpApi.myGoodsSpecialList, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "big_type": bigType,
      "small_type": smallType
    });
    print("信息----${response.data}");
    return response.data["data"]
        .map<MyGoodsSpecialModel>((item) => MyGoodsSpecialModel.fromJson(item))
        .toList();
  }

  //专辑下的作品
  static Future specialListByAlbumId({
    required int page,
    required int pageSize,
    required String id,
  }) async {
    var response =
        await http.get(HttpApi.specialListByAlbumId, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "id": id,
    });
    return response.data["data"]
        .map<MyGoodsSpecialModel>((item) => MyGoodsSpecialModel.fromJson(item))
        .toList();
  }

  //首发
  static Future goodsSpecialList(
      {required int page,
      required int pageSize,
      required int isHot,
      int isNow = 0,
      required int type,
      int will = 2,
      int? orderType,
      String? ID = "",
      String? search}) async {
    var response = await http.get(HttpApi.goodsSpecialList, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "is_hot": isHot,
      "is_now": isNow,
      "type": type,
      "order_type": orderType,
      "is_will_status": will,
      "member_id": ID,
      "search": search
    });
    return response.data["data"]
        .map<GoodsSpecialList>((item) => GoodsSpecialList.fromJson(item))
        .toList();
  }

  //批量购买藏品
  static batchBuyCollection({
    String? specialId,
    String? count,
  }) async {
    await http.post(HttpApi.batchBuyCollection, data: {
      "special_id": specialId,
      "count": count,
    });
  }

  //转售列表
  static Future goodsList(
      {required int page,
      required int pageSize,
      required int type,
      required int orderType,
      String? search}) async {
    var response = await http.post(HttpApi.goodsList, data: {
      "page": page,
      "page_size": pageSize,
      "type": type,
      "order_type": orderType,
      "search": search
    });
    return response.data["list"]["data"]
        .map<GoodsList>((item) => GoodsList.fromJson(item))
        .toList();
  }

  //账单列表
  static Future billList(
      {required int type, required int page, required int pageSize}) async {
    var response = await http.get(HttpApi.billList, queryParameters: {
      "bill_type_pid": type,
      // "now_page": page,
      "page": page,
      "page_num": pageSize,
      "bill_type_id": "1"
    });
    print("----账单列表--${response}");
    return response.data["list"]["data"]
        .map<BillListModel>((item) => BillListModel.fromJson(item))
        .toList();
  }

  //商品专辑分类列表
  static goodsSpecialTypeList() async {
    var response = await http.get(
      HttpApi.goodsSpecialTypeList,
    );
    return response.data
        .map<SpecialTypeModel>((item) => SpecialTypeModel.fromJson(item))
        .toList();
  }

  //藏品添加 special_type_id 专题分类ID
  static createSpecialCang({
    String? ID,
    String? typeId,
    String? albumId,
    String? title,
    String? count,
    String? detail,
    String? address,
    String? thumb,
  }) async {
    print("传惨--${{
      "id": ID,
      "special_type_id": typeId,
      "special_album_id": albumId,
      "title": title,
      "count": count,
      "detail": detail,
      "resources_address": address,
      "thumb": thumb
    }}");
    await http.post(HttpApi.createSpecialCang, data: {
      "id": ID,
      "special_type_id": typeId,
      "special_album_id": albumId,
      "title": title,
      "count": count,
      "detail": detail,
      "resources_address": address,
      "thumb": thumb
    });
  }

  //盲盒添加 special_type_id 专题分类ID
  static createSpecialMang({
    String? ID,
    String? typeId,
    String? albumId,
    String? title,
    int? count,
    String? thumb,
    String? detail,
    String? story,
    String? blindBox,
  }) async {
    await http.post(HttpApi.createSpecialMang, data: {
      "id": ID,
      "special_type_id": typeId,
      "special_album_id": albumId,
      "title": title,
      "count": count,
      "thumb": thumb,
      "detail": detail,
      "story": story,
      "blind_box_list": blindBox
    });
  }

  //单个盲盒/藏品下架
  static goodsSoldOut({
    String? id,
  }) async {
    await http.post(HttpApi.goodsSoldOut, data: {
      "id": id,
    });
  }

  //整体盲盒/藏品下架
  static soldOut({
    String? id,
  }) async {
    await http.post(HttpApi.soldOut, data: {
      "id": id,
    });
  }

  //购买整体盲盒
  static Future purchaseGoodsMang(
    int id,
  ) async {
    var response = await http.post(HttpApi.purchaseGoodsMang, data: {
      "id": id,
    });
    return GoodsMangModel.fromJson(response.data);
  }

  //抢购藏品
  static Future purchaseGoods(
    int id,
  ) async {
    var response = await http.post(HttpApi.purchaseGoods, data: {
      "id": id,
    });
    return GoodsMangModel.fromJson(response.data);
  }

  //整体藏品转售
  static specialChange(String id, String price, String will,
      {String time = ""}) async {
    await http.post(HttpApi.specialChange, data: {
      "id": id,
      "price": price,
      "is_will_status": will,
      "sell_time": time
    });
  }

  //整体盲盒转售
  static specialMangChange(String id, String price, String will,
      {String time = ""}) async {
    await http.post(HttpApi.specialMangChange, data: {
      "id": id,
      "price": price,
      "is_will_status": will,
      "sell_time": time
    });
  }

  //实名认证
  static authReal(
      String identityz, String identityf, String name, String id_card) async {
    await http.post(HttpApi.authReal, data: {
      "identity_z": identityz,
      "identity_f": identityf,
      "idCard": id_card,
      "name": name
    });
  }

  //支付宝
  static paymentCertification(
    String name,
    String account,
  ) async {
    await http.post(HttpApi.paymentCertification, data: {
      "alipay_name": name,
      "alipay_account": account,
    });
  }

  //薪资认证
  static memberExtend() async {
    var response = await http.post(HttpApi.memberExtend, data: {});
    return MemberExtendModel.fromJson(response.data["list"]);
  }

  //转售
  static goodsChange(String id, String price) async {
    await http.post(HttpApi.goodsChange, data: {"id": id, "price": price});
  }

  //服务费
  static getFeeConfig() async {
    var response = await http.get(HttpApi.getFeeConfig, queryParameters: {});
    return FeeConfigModel.fromJson(response.data);
  }

  //专辑列表
  static specialAlbumList({
    required int page,
    required int pageSize,
    int type = 0,
    int? status,
  }) async {
    var response = await http.get(HttpApi.specialAlbumList, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "type": type,
      "status": status ?? ""
    });
    return response.data["data"]
        .map<AlbumListModel>((item) => AlbumListModel.fromJson(item))
        .toList();
  }

  //根据商品ID获取要支付的最新订单
  // static Future getOrderInfoByGoodsId({required int id,}) async {
  //   var response = await http.post(HttpApi.getOrderInfoByGoodsId,
  //       data: {"goods_id": id,});
  //   return response.data;
  // }

  //专辑添加/修改
  static specialAlbumSave(
      {String? id,
      required String titile,
      required String thumb,
      required String rate}) async {
    await http.post(HttpApi.specialAlbumSave,
        data: {"id": id ?? "", "titile": titile, "thumb": thumb, "rate": rate});
  }

  //专辑删除
  static specialAlbumDelete({
    required int id,
  }) async {
    await http.post(HttpApi.specialAlbumDelete, data: {
      "id": id,
    });
  }

  //藏品详情
  static goodsInfo({
    required String id,
  }) async {
    var response = await http.post(HttpApi.goodsInfo, data: {
      "id": id,
    });
    print("藏品详情11----$id}");
    return GoodsInfo.fromJson(response.data);
  }

  //整体藏品详情
  static collectionInfo({
    required String id,
  }) async {
    var response = await http.post(HttpApi.collectionInfo, data: {
      "id": id,
    });
    print("整体藏品详情----${response.data}");
    return GoodsInfo.fromJson(response.data);
  }

  //整体藏品详情首发
  static specialFirstGoods({
    required String id,
  }) async {
    var response = await http.post(HttpApi.specialFirstGoods, data: {
      "special_id": id,
    });
    print("整体藏品详情----${response.data}");
    return GoodsInfo.fromJson(response.data);
  }

  //创作者信息
  static getChuangUserInfo(String id) async {
    var response = await http.get(HttpApi.getChuangUserInfo, queryParameters: {
      "id": id,
    });
    print("请求参数---${response.data}");
    return UserInfoModel.fromJson(response.data);
  }


  //铸造
  static specialCast3({
    required String id,
  }) async {
    var response = await http.post(HttpApi.specialCast, data: {
      "id": id,
      "pay_type":3
    });
    return response.data;
  }

  //铸造
  static specialCast({
    required String id,
  }) async {
    var response = await http.post(HttpApi.specialCast, data: {
      "id": id,
      "pay_type":1
    });
    return SpecialCastModel.fromJson(response.data);
  }

  //铸造 微信
  static specialWXCast({
    required String id,
  }) async {
    var response = await http.post(HttpApi.specialCast, data: {
      "id": id,
      "pay_type":2
    });
    return response.data;
  }

  //买家发起支付
  static goodsPay3({
    required String id
  }) async {
    var response = await http.post(HttpApi.goodsPay, data: {
      "id": id,
      "pay_type":3
    });
    return response.data;
  }

  //买家发起支付
  static goodsPay({
    required String id
  }) async {
    var response = await http.post(HttpApi.goodsPay, data: {
      "id": id,
      "pay_type":1
    });
    return SpecialCastModel.fromJson(response.data);
  }
  static dynamic goodsWXPay({
    required String id
  }) async {
    var response = await http.post(HttpApi.goodsPay, data: {
      "id": id,
      "pay_type":2
    });
    return response.data;
  }

  //提现
  static transfer({
    required String money,
  }) async {
    print("提现---${money}");
    await http.post(HttpApi.transfer, data: {
      "cash_money": money,
    });
  }

  //藏品交易记录
  static goodsLog({
    required int page,
    required int pageSize,
    required int id,
  }) async {
    var response = await http.post(HttpApi.goodsLog,
        data: {"page": page, "page_size": pageSize, "id": id});
    return response.data["list"]["data"]
        .map<OrderList>((item) => OrderList.fromJson(item))
        .toList();
  }

  //版税记录
  static getChuangRateFeeList(
      {required int page,
      required int pageSize,
      required String id,
      required String albumId}) async {
    var response =
        await http.get(HttpApi.getChuangRateFeeList, queryParameters: {
      "page": page,
      "page_size": pageSize,
      "special_id": id,
      "special_album_id": albumId
    });
    return response.data["data"]
        .map<ChuangRateFeeListModel>(
            (item) => ChuangRateFeeListModel.fromJson(item))
        .toList();
  }

  //盲盒抽取记录
  static specialMangLog({
    required int page,
    required int pageSize,
    required int id,
  }) async {
    var response = await http.post(HttpApi.specialMangLog,
        data: {"page": page, "page_size": pageSize, "id": id});
    return response.data["list"]["data"]
        .map<OrderList>((item) => OrderList.fromJson(item))
        .toList();
  }

  //盲盒详情
  static mangInfo({
    required String id,
  }) async {
    var response = await http.post(HttpApi.mangInfo, data: {"id": id});
    print("盲盒详情----${response.data}");
    return BoxInfoModel.fromJson(response.data);
  }

  //打开盲盒
  static openBlindBox({
    required String id,
  }) async {
    var response = await http.post(HttpApi.openBlindBox, data: {"id": id});
    return OpenBoxModel.fromJson(response.data);
  }

  //单个盲盒详情
  static goodsInfoMang({
    required String id,
  }) async {
    var response = await http.post(HttpApi.goodsInfoMang, data: {"id": id});
    print("单个盲盒----${id}");
    return BoxInfoModel.fromJson(response.data);
  }

  //获取邀请页面加载项（配置等）
  static getInviteInfo({
    required String id,
  }) async {
    var response =
        await http.get(HttpApi.getInviteInfo, queryParameters: {"id": id});
    LogUtil.v("信息---${response}");
    return InviteInfoModel.fromJson(response.data);
  }

  //
  // //分享图片
  // static Future sharePosterRanking(int uid) async {
  //   var response =
  //       await http.post(HttpApi.sharePosterRanking, data: {"uid": uid});
  //   return response.data['poster_path'];
  // }
  //
  // //下载海报
  // static Future sharePoster(int index) async {
  //   var response =
  //   await http.post(HttpApi.sharePoster, data: {"index": index});
  //   return response.data["poster_path"];
  // }
  //
  //
  // //帮助指南
  // static Future noticeList(int page) async {
  //   var response = await http.get(HttpApi.NoticeList,
  //       queryParameters: {"page": page, "list_rows": 10, "type": 1});
  //   return response.data['list']["data"]
  //       .map<ArticleList>((item) => ArticleList.fromJson(item))
  //       .toList();
  // }
  //
  // //公告 帮助指南 详情
  // static Future getNoticeDetail(int id) async {
  //   var response =
  //       await http.get(HttpApi.getNoticeDetail, queryParameters: {"id": id});
  //   return ArticleList.fromJson(response.data);
  // }
  //
  //
  //图片上传
  static fileUpload(FormData formData) async {
    var response = await http.post(
      HttpApi.fileUpload,
      data: formData,
    );
    return response.data["url"];
  }

  //
  //
  // //最新公告
  // static Future ReadNotice() async {
  //   var response = await http.post(HttpApi.readNotice);
  //   return NoticeData.fromJson(response.data);
  // }
  //
  // //获取运营设置
  // static Future Readrunconfig() async {
  //   var response = await http.post(HttpApi.readrunconfig);
  //   return Currency.fromJson(response.data);
  // }
  //
  //
  //应用升级
  static Future<AppInfo> upindex(version) async {
    print("升级请求");
    var response = await http.get(HttpApi.upindex, queryParameters: {
      "client_type": Platform.isAndroid ? 1 : 2,
      "client_version": version
    });
    print("升级返回--${response}");
    return AppInfo.fromJson(response.data["upgrade"]);
  }

  //
  // //公告列表
  // static Future article({int? page_size, int? page, int? type}) async {
  //   var response = await http.get(HttpApi.NoticeList, queryParameters: {
  //     "page_size": page_size ?? "",
  //     "page": page ?? "",
  //     "type": type,
  //   });
  //   return response.data['list']["data"]
  //       .map<Article>((item) => Article.fromJson(item))
  //       .toList();
  // }
  //
  //
  //
  //根据name获取配置信息
  static Future getConfigNameDetail(String name) async {
    var response = await http
        .get(HttpApi.getConfigNameDetail, queryParameters: {"name": name});
    return response.data['info']
        .map<ConfigNameDetail>((item) => ConfigNameDetail.fromJson(item))
        .toList();
  }

  //
  // //协议
  // static Future getConfigDetail() async {
  //   var response =
  //       await http.get(HttpApi.getConfigDetail, queryParameters: {"id": "11"});
  //   return ConfigNameDetail.fromJson(response.data);
  // }
  //
  //
  //
  // //手机区号
  // static Future mobileCodeList() async {
  //   var response = await http.get(
  //     HttpApi.mobileCode,
  //   );
  //
  //   return response.data['sms_list']
  //       .map<MobileCode>((item) => MobileCode.fromJson(item))
  //       .toList();
  // }

  /// 登出
  static logout() async {
    /// 自动移除cookie
    await http.get(HttpApi.authLogin);
  }

  //获取帮助中心问题列表
  static getQuestionList() async {
    var response = await http.get(HttpApi.getQuestionList);
    LogUtil.v("信息---${response}");
    return HelpData.fromJson(response.data);
  }

  //获取帮助中心问题详情信息
  static Future getNoticeDetail(int id) async {
    var response =
        await http.get(HttpApi.getNoticeDetail, queryParameters: {"id": id});
    return HelpDetaiData.fromJson(response.data);
  }

  //获取邀请活动中，新增加得邀请人列表
  static getInviteUserList({required String id, required int page}) async {
    var response = await http.get(HttpApi.getInviteUserList,
        queryParameters: {"id": id, "page": page});
    return response.data["data"]
        .map<InviteUserList>((item) => InviteUserList.fromJson(item))
        .toList();
  }

  //开盲盒接口
  static openPackage({
    required String id,
  }) async {
    var response =
        await http.get(HttpApi.openPackage, queryParameters: {"id": id});
    return InviteUserList.fromJson(response.data);
  }

  //获取图像验证码
  static getImageCode(String phone) async {
    var response =
        await http.post(HttpApi.getImgCode, data: {"username": phone});
    return response.data["url"];
  }
}
